package com.araaya.eslimsmobile.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.araaya.eslimsmobile.util.ElandConstant;

import android.util.Log;
import android.widget.Toast;

public class WebCaller {

	public InputStream callService(String geturl) {
		InputStream in = null;
		String text = "TestAndroid";

		try {
			sendDataByPostMethod();
			/*
			 * URL url = new URL(geturl); URLConnection conn =
			 * url.openConnection();
			 * 
			 * HttpURLConnection httpConn = (HttpURLConnection) conn; //
			 * httpConn.setRequestMethod("POST");
			 * httpConn.setRequestMethod("GET"); httpConn.setDoInput(true);
			 * httpConn.setDoOutput(true); httpConn.connect();
			 * 
			 * // DataOutputStream dataStream = new DataOutputStream( //
			 * conn.getOutputStream());
			 * 
			 * // dataStream.writeBytes(text); // dataStream.flush(); //
			 * dataStream.close();
			 * 
			 * int responseCode = httpConn.getResponseCode(); if (responseCode
			 * == HttpURLConnection.HTTP_OK) { in = httpConn.getInputStream(); }
			 */
		} catch (Exception ex) {
			// display("Error: Not not connect");
			// Toast.makeText(getApplicationContext(), "Error: Not not connect",
			// Toast.LENGTH_LONG).show();
			in = null;
		}
		return in;
	}

	public String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	// send By POST
	public void sendDataByPostMethod() {

		// Add data to be send.
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
				3);
		nameValuePairs
				.add(new BasicNameValuePair("maction",
						"80.120705,6.7987313,62.2 80.120834,6.7987604,54.2 80.12164,6.799118,39.5 "));
		nameValuePairs.add(new BasicNameValuePair("userName", "orlm"));
		nameValuePairs.add(new BasicNameValuePair("Password", "password"));

		sendData(nameValuePairs);
	}

	public String sendData(ArrayList<NameValuePair> data) {
		//InputStream in = null;
		String result="fail";
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(ElandConstant.Mobile_URL);
			httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(data));
			HttpResponse response = httpclient.execute(httppost);
			Log.d("postData", response.getStatusLine().toString());
			
			
			// handle the response
			InputStream ips  = response.getEntity().getContent();
	        BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
	        if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK)
	        {
	            throw new Exception(response.getStatusLine().getReasonPhrase());
	        }
	        StringBuilder sb = new StringBuilder();
	        String s;
	        while(true )
	        {
	            s = buf.readLine();
	            if(s==null || s.length()==0)
	                break;
	            sb.append(s);

	        }
	        buf.close();
	        ips.close();
	        result= sb.toString();
	        Log.d("RESULT", result);


		} catch (Exception e) {
			Log.d("log_tag", "Error:  " + e.toString());
			result="fail";
		}
		return result;
	}

}
